<?php
namespace api\service;

use api\service\interfaces\IAppStatAccountService;
use herosphp\bean\Beans;
use herosphp\core\Loader;
use herosphp\core\Log;
use herosphp\utils\AjaxResult;
use payment\models\SignVo;
use payment\utils\SerialNumberUtils;

Loader::import('api.service.interfaces.IAppStatAccountService');

/**
 * app结算户账户服务接口实现
 * @author yangjian<yangjian102621@gmail.com>
 */
class AppStatAccountService implements IAppStatAccountService {

    /**
     * @see IAppStatAccountService::sign
     */
    public function sign($appid, $install_id,  $userid)
    {
        //参数过滤
        if ( !$appid || !$install_id || !$userid ) return AjaxResult::fail('缺少必填参数.');
        //判断该用户是否合法
        $userService = Beans::get('user.user.service');
        $user = $userService->getItem($userid);
        if ( $user['ischecked'] != 2 ) return AjaxResult::fail('生成结算户失败, 用户未实名认证.');
        if ( $user['sign_status'] != 1 ) return AjaxResult::fail('生成结算户失败, 用户未签约.');
        if ( $user['type'] != 2 ) return AjaxResult::fail('生成结算户失败，该用户非企业用户.');

        //对于同一个APP， 每个大塑支付会员只能开通一次
        $model = Loader::model('appStatAccount');
        $item = $model->getItem(array('appid' => $appid, 'userid' => $userid));
        if ( $item != false ) {
            Log::info("警告：重复开通MPE，每个支付账号只能开通一次MPE");
            $resVo = AjaxResult::fail(LANG('您在重复开通MPE，每个支付账号只能开通一次MPE.'));
            $resVo->putItem($item);
            return $resVo;
        }

        //如果没有签约过，则继续签约
        if ( is_array($user['company']) ) {
            $company = $user['company'];

            $paywayService = Beans::get('payment.payway.service');
            $payway = $paywayService->getItem($user['payway']);
            //先创建MPE的虚拟账户
            $signVo = new SignVo();
            $signVo->setBankName($payway['key']); //支付通道
            $signVo->setSubAccountName($company['legal_person']); //虚拟帐号户名
            $signVo->setContactName($company['legal_person']); //联系人
            $signVo->setContactPhone($company['mobile']); //联系电话
            $signVo->setRequestNumber(SerialNumberUtils::generate($payway['serial_prefix'])); //流水号
            $signVo->setRemark('广发注册接口-注册APP结算户');
            $signVo->setType(SignVo::SIGN);
            $paymentService = Beans::get('payment.payment.service');
            $res = $paymentService->sign($signVo);
            if ( $res === false ) return AjaxResult::fail('生成结算户失败.');

            //记录app的结算户信息
            $model = Loader::model('AppStatAccount');
            $data['install_id'] = $install_id;
            $data['appid'] = $appid;
            $data['userid'] = $userid;
            $data['virtual_name'] = $signVo->getSubAccountName();
            $data['virtual_account'] = $res['subAccount'];
            $data['contacter'] = $signVo->getContactName();
            $data['mobile'] = $signVo->getContactPhone();
            $data['addtime'] = date('Y-m-d H:i:s');
            if ( $model->add($data) ) {
                $resVo = AjaxResult::success(LANG('DEAL_SUCCESS'));
                $resVo->putItem($data);
                return $resVo;
            }
        }

        return AjaxResult::fail(LANG('DEAL_FAILD'));
     }
}
